#include "ikvm_args.hpp"

#include <getopt.h>
#include <rfb/rfb.h>
#include <stdio.h>
#include <stdlib.h>

namespace ikvm
{
Args::Args(int argc, char* argv[]) :
    frameRate(30), subsampling(0), calcFrameCRC{false}, commandLine(argc, argv)
{
    int option;
    const char* opts = "f:s:h:k:p:u:v:c";
    struct option lopts[] = {
        {"frameRate", 1, 0, 'f'},       // 设置帧率的选项
        {"subsampling", 1, 0, 's'},     // 设置子采样的选项：1 = YUV420, 0 = YUV444
        {"help", 0, 0, 'h'},            // 帮助选项
        {"keyboard", 1, 0, 'k'},        // 指定键盘设备路径的选项：/dev/hidg0
        {"mouse", 1, 0, 'p'},           // 指定鼠标设备路径的选项：/dev/hidg1
        {"udcName", 1, 0, 'u'},         // 指定UDC名称的选项：1e6a0000.usb-vhub:pX
        {"videoDevice", 1, 0, 'v'},     // 指定视频设备路径的选项：/dev/video0
        {"calcCRC", 0, 0, 'c'},         // 启用CRC计算的选项
        {0, 0, 0, 0}};                  // 选项结束
    // /usr/bin/obmc-ikvm -v /dev/video0 -k /dev/hidg0 -p /dev/hidg1 
    while ((option = getopt_long(argc, argv, opts, lopts, NULL)) != -1)
    {
        switch (option)
        {
            case 'f':
                frameRate = (int)strtol(optarg, NULL, 0);  // 解析帧率
                if (frameRate < 0 || frameRate > 60)       // 验证帧率范围
                    frameRate = 30;                        // 如果超出范围则重置为默认值
                break;
            case 's':
                subsampling = (int)strtol(optarg, NULL, 0);  // 解析子采样
                if (subsampling < 0 || subsampling > 1)      // 验证子采样范围
                    subsampling = 0;                         // 如果超出范围则重置为默认值
                break;
            case 'h':
                printUsage();  // 打印使用信息
                exit(0);       // 打印帮助信息后退出程序
            case 'k':
                keyboardPath = std::string(optarg);  // 设置键盘设备路径
                break;
            case 'p':
                pointerPath = std::string(optarg);   // 设置鼠标设备路径
                break;
            case 'u':
                udcName = std::string(optarg);       // 设置UDC名称
                break;
            case 'v':
                videoPath = std::string(optarg);     // 设置视频设备路径
                break;
            case 'c':
                calcFrameCRC = true;                 // 启用CRC计算
                break;
        }
    }
}

void Args::printUsage()
{
    // use fprintf(stderr to match rfbUsage()
    fprintf(stderr, "OpenBMC IKVM daemon\n");
    fprintf(stderr, "Usage: obmc-ikvm [options]\n");
    fprintf(stderr, "-f frame rate          try this frame rate\n");
    fprintf(stderr, "-s subsampling         try this subsampling\n");
    fprintf(stderr, "-h, --help             show this message and exit\n");
    fprintf(stderr, "-k device              HID keyboard gadget device\n");
    fprintf(stderr, "-p device              HID mouse gadget device\n");
    fprintf(stderr,
            "-u udc name            UDC that HID gadget will connect to\n");
    fprintf(stderr, "-v device              V4L2 device\n");
    fprintf(
        stderr,
        "-c, --calcCRC          Calculate CRC for each frame to save bandwidth\n");
    rfbUsage();
}

} // namespace ikvm
